var appPath = getAppPath();

$(function(){
	initAreaCombo();
	initAddIcDtCombo();
	initRank();
	initRefDtCombo();
});

//初始化 区域选择
function initAreaCombo(){
	$("#queryApplyProv").combobox({
		url:appPath+'/itbCom/getAreaForCombo.action?parent=1&blkFlg=1',// 中国
		valueField:'id',
		textField:'name',
		onSelect:function(selProv){
			$("#queryApplyCity").combobox("clear");
			$("#queryApplyDist").combobox("clear");
			$("#queryApplyCity").combobox("loadData",{});
			$("#queryApplyDist").combobox("loadData",{});
			// 市
			var parentId=selProv.id;
			if(Number(parentId)>0){
				$("#queryApplyCity").combobox({
					url:appPath+'/itbCom/getAreaForCombo.action?parent='+parentId+'&blkFlg=1',
					valueField:'id',
					textField:'name',
					onSelect:function(selCity){
						$("#queryApplyDist").combobox("clear");
						$("#queryApplyDist").combobox("loadData",{});
						// 区
						var parentId2=selCity.id;
						if(Number(parentId2)>0){
							$("#queryApplyDist").combobox({
								url:appPath+'/itbCom/getAreaForCombo.action?parent='+parentId2+'&blkFlg=1',
								valueField:'id',
								textField:'name',
								onSelect:function(selDist){
									// TODO
								}
							});
						}
					}
				});
			}
		}
	});
}

	
//刷新排行榜年份
function initRefDtCombo() {
	var dataY = [];
	for(var i=2016; i<2100; i++) {
		dataY.push({ "text": i, "id": i });
	}
	$("#refYear").combobox({
		data: dataY,
		valueField:'id',
		textField:'text',
		onLoadSuccess: function(param) {
			$("#refYear").combobox('select',2017);
        }
	});
	
	var dataM = [];
	for(var i=1; i<=12; i++) {
		dataM.push({ "text": i, "id": i });
	}
	$("#refMonth").combobox({
		data: dataM,
		valueField:'id',
		textField:'text',
		onLoadSuccess: function(param) {
			$("#refMonth").combobox('select',1);
        }
	});
}

//首页年份
function initAddIcDtCombo() {
	var dataY = [{'id':0,'text':"不限"}];
	for(var i=2016; i<2100; i++) {
		dataY.push({ "text": i, "id": i });
	}
	$("#addIncomeYear").combobox({
		data: dataY,
		valueField:'id',
		textField:'text',
		onLoadSuccess: function(param) {
			$("#addIncomeYear").combobox('select',0);
		},
		onSelect : function(sel){
			var id = sel.id;
			if(id==0){
				$("#addIncomeMonth").combobox('select',0);
			}else{
				$("#addIncomeMonth").combobox('select',1);
			}
		}
	});
	
	var dataM = [{'id':0,'text':"不限"}];
	for(var i=1; i<=12; i++) {
		dataM.push({ "text": i, "id": i });
	}
	$("#addIncomeMonth").combobox({
		data: dataM,
		valueField:'id',
		textField:'text',
		onLoadSuccess: function(param) {
			$("#addIncomeMonth").combobox('select',0);
		}
	});
}

/**获取排行信息 需要参数*/
function getRankParm(){
	var obj={};
	var dis = $("#queryApplyDist").combobox('getValue');//获取地区
	var city = $("#queryApplyCity").combobox('getValue');//获取城市
	var prov = $("#queryApplyProv").combobox('getValue');//获取省份
	var year=$("#addIncomeYear").combobox('getValue');//获取年份
	var month=$("#addIncomeMonth").combobox('getValue');//获取月份
	var mobile =$("#mobile").val();
	
	if(Number(year)>0) obj.year=year;
	if(Number(month)>0 ) obj.month=month;
	if(Number(dis)) obj.dist=dis;
	if(Number(mobile)>0) obj.mobile = mobile;
	if(Number(prov)>0) obj.prov = prov;
	if(Number(city)>0) obj.city = city;
	return obj;
}
/**获取排行信息 需要参数
 */
function getRankParm1(){
	var obj={};
	var year='2017';
	var month='1';
	var dist='110100';
	if(year && year.length>0) obj.year=year;
	if(month && month.length>0) obj.month=month;
	if(dist && dist.length>0) obj.dist=dist;
	return obj;
}

/**加载销售
 * 排行*/
function initRank(){
	var num=1;
	$("#rankGrid").datagrid({
		url:appPath+"/itbRank/getPartnerSaleRank.action",
		queryParams:getRankParm(),
	    height:'full',
	    striped:true,
	    fitColumns:true,
	    singleSelect:false,
	    selectOnCheck:true,
	    checkOnSelect:true,
	    columns:[[
	            {field:'rank',title:'排名',width:30,align:'center',sortable:false,
	            	formatter:function(value,rowData,rowIndex){
	            		if(Number(value||'0')==1){
	            			return '<span style="color:red;">'+value+'</span>';
	            		}else{
	            			return '<span style="color:green;">'+value+'</span>';;
	            		}
	            	}  
	            },
	  	        {field:'year',title:'年份',width:20,align:'center',sortable:false,
	  	        	formatter:function(value,rowData,rowIndex){
	  	        		return value;
  	        		}
	  	        },
	  	        {field:'month',title:'月份',width:20,align:'center',sortable:false,
	  	        	formatter:function(value,rowData,rowIndex){
	  	        		return value;
  	        		}
	  	        },
	  	        {field:'pname',title:'省会',width:40,align:'center',sortable:false,
	            	formatter:function(value,rowData,rowIndex){
	  	        		return value;
  	        		}
	  	        },
	  	        {field:'cname',title:'市区',width:40,align:'center',sortable:false,
	  	        	formatter:function(value,rowData,rowIndex){
	  	        		return value;
  	        		}
	  	        },
	  	        {field:'dname',title:'城区',width:40,align:'center',sortable:false,
	  	        	formatter:function(value,rowData,rowIndex){
	  	        		return value;
  	        		}
	  	        },
	  	        {field:'partnerId',title:'合伙人ID',width:30,align:'center',sortable:false,
	  	        	formatter:function(value,rowData,rowIndex){
	            		  if(num==rowData.rank){
	            			  return '<span style="color:red;">'+value+'</span>';
	            		  }
	            		  return value||'';
	            	  }
	  	        },
	  	        {field:'name', title:'姓名', width:30, align:'center',sortable:false,
	  	        	formatter:function(value,rowData,rowIndex){
	  	        		if(num==rowData.rank){
	  	        			return '<span style="color:red;">'+value+'</span>';
	  	        		}
	  	        		return value||'';
	  	        	}
	  	        },
	  	        {field:'mobile',title:'注册手机号',width:40,align:'center',sortable:false,
	  	        	formatter:function(value,rowData,rowIndex){
	            		  if(value && num==rowData.rank){
	            			  return '<span style="color:red;">'+value+'</span>';
	            		  }
	            		  return value||'<span style="color:#B5B5B5;">暂未填写</span>';
	            	  }
	  	        },
	  	        {field:'saleFee',title:'销售额',width:30, align:'right',sortable:false,
	  	        	formatter:function(value,rowData,rowIndex){
	  	        		if(value){
	  	        			return '<span style="color:blue;">'+(value)/100+' 元</span>';
	  	        		}else{
	  	        			return '';
	  	        		}
	  	        	}
	  	        },
	  	        {field:'income',title:'收入',width:30, align:'right',sortable:false,
	  	        	formatter:function(value,rowData,rowIndex){
	  	        		if(value){
	  	        			return '<span style="color:green;">'+(value)/100+' 元</span>';
	  	        		}else{
	  	        			return '';
	  	        		}
	  	        	}
	  	        },
	  	        {field:'desc',title:'备注信息',width:40, align:'center', sortable:false,
	  	        	formatter:function(value,rowData,rowIndex){
	  	        		return value||'';
	  	        	}
	  	        },
	  	        {field:'create_time',title:'创建时间', width:70, align:'center', sortable:false},
	  	        {field:'update_time',title:'修改时间', width:70, align:'center', sortable:false}
	  	    ]],
		pagination:true,
		rownumbers:true,
		toolbar:[{
				text:'刷新排名',
				iconCls:'icon-reload',
				handler:function(){
				refresh();
				}}]
	});
}

//刷新排名
function refresh(){
	$("#retSuccess").html('');
	$("#retInfo").html('');
	$('#refresh').window({
		title:'刷新排名信息',
		iconCls:'icon-reload',
		modal: true,
		shadow: true,
		collapsible:false,
		minimizable:false,
		maximizable:false
	});
	$('#refresh').window('move');
	$('#refresh').window('open');
}


function doRefresh(){
	$("#retSuccess").html('');
	$("#retInfo").html('');
	$.loading(true);
	var year=$("#refYear").combobox('getValue');
	if(year==null || year.length<=0){
		$.messager.alert('提示信息','选择一个年份！','info');
		return;
	}
	var month=$("#refMonth").combobox('getValue');
	if(month==null || month.length<=0){
		$.messager.alert('提示信息','请选择一个月份！','info');
		return;
	}
	var url=getApiServer()+ "/callFunc/refreshSaleRankInfo?y="+year+"&m="+month+"";
	$.post(url,function(data){
		if(data.success==1) {
			var st = data.data;
			$("#retSuccess").html('刷新成功!');
			$("#retInfo").html(JSON.stringify(st));
			$.loading(false);
		} else {
			$("#retSuccess").html('刷新失败!');
			$.loading(false);
		}
	});
}


/**查询数据
 */
function doQuery(){
	$("#rankGrid").datagrid('unselectAll');
	$("#rankGrid").datagrid('load', getRankParm());
}


function getLastDay(y,m) {
	var dt = new Date(y,m,1);
	var cdt = new Date(dt.getTime()-1000*3600*24);
	return $.DateUtil.format(cdt,'yyyy-MM-dd');
}